package com.matt.repository.impl;

import com.matt.domain.course.CoursePlanClasses;
import com.matt.domain.nursery.NurseryScheduleRecord;
import com.matt.foundation.DO.course.CoursePlanClassesDDO;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.CoursePlanClassesNativeRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by lch.
 * form com.matt
 * on 2018/11/27
 * *******************************
 */
@Service
public class CoursePlanClassesNativeRepositoryImpl extends BaseNativeSqlRepository implements CoursePlanClassesNativeRepository {

    @Override
    public NurseryScheduleRecord findByCourseDateAndClassesIdAndCategory(String courseDateStr, String classesId, int category) {
        String querySql = String.format("select cpc.courseName as activityContent,cpc.beginTime as beginDate,cpc.endTime as endDate " +
                "from course_plan_classes cpc where date(cpc.courseDate)='%s' and cpc.classesId='%s' and cpc.category='%s' order by cpc.beginTime asc",courseDateStr,classesId,category);
        List<NurseryScheduleRecord> list = sqlObjectList(querySql,NurseryScheduleRecord.class);
        if (list.size()>0)
            return list.get(0)!=null ? list.get(0):null;
        return null;
    }

    @Override
    public List<CoursePlanClasses> findByCourseDateAndClassesIdAndCategoryOrderByBeginTimeDesc(Date lessonDate, String classesId, int category) {
        String querySql = String.format("select cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
                " from course_plan_classes cpc where date(cpc.courseDate)=date('%s') and cpc.classesId='%s' order by cpc.beginTime asc", DateUtil.convertStr(lessonDate),classesId);
        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class);
       return list;
    }

    @Override
    public List<CoursePlanClasses> findByKXCourseDateAndClassesIdAndCategoryOrderByBeginTimeDesc(Date lessonDate, String classesId, int category) {
        String querySql = String.format("select ct.type as type,cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
                " from course_plan_classes cpc,course_template_4 ct where cpc.courseId=ct.courseId and date(cpc.courseDate)=date('%s') and cpc.classesId='%s' order by cpc.beginTime asc", DateUtil.convertStr(lessonDate),classesId);
        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class); 
       return list;
    }
    @Override
    public List<CoursePlanClasses> findByClassesIdAndCategoryOrderByBeginTimeDesc(Date beginDate, Date endDate, String classesId, int category) {
        String querySql = String.format("select cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
                " from course_plan_classes cpc where date(cpc.courseDate)>=date('%s') and date(cpc.courseDate)<=date('%s') " +
                " and cpc.classesId='%s' and cpc.category='%s' order by cpc.beginTime asc", DateUtil.convertStr(beginDate),DateUtil.convertStr(endDate),classesId,category);
        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class);
        return list;
    }

    @Override
    public CoursePlanClassesDDO findByCoursePlanClassesId(String planId) {
        String querySql = String.format("select a.id as classesCoursePlanId,a.courseId from course_plan_classes a where a.id ='%s'", planId);
        List<CoursePlanClassesDDO> list = sqlObjectList(querySql,CoursePlanClassesDDO.class);
        if (list.size() > 0)
            return list.get(0) == null ? null : list.get(0);
        return null;
    }

    @Override
    public List<CoursePlanClasses> findByCourseDateAndCategoryOrderByBeginTimeDesc(Date lessonDate, int category) {
        String querySql = String.format("select cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
                " from course_plan_classes cpc where date(cpc.courseDate)=date('%s')  and cpc.category='%s' order by cpc.beginTime asc", DateUtil.convertStr(lessonDate),category);
        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class);
       return list;
    }

	@Override
	public List<CoursePlanClasses> findByCourseDateAndClassesIdAndBeginTimeOrderByBeginTimeDesc(
			Date lessonDate, String classesId,Date beginTime) {
		// TODO Auto-generated method stub
		 String querySql = String.format("select cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
	                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
	                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
	                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
	                " from course_plan_classes cpc where date(cpc.courseDate)=date('%s') and cpc.classesId='%s' and date(cpc.beginTime)=date('%s')  order by cpc.beginTime asc", DateUtil.convertStr(lessonDate),classesId, DateUtil.convertStr(beginTime));
	        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class);
	        return list;
	}

	@Override
	public List<CoursePlanClasses> findByCourseDateAndClassesIdOrderByBeginTimeDesc(Date lessonDate, String classesId) {
		// TODO Auto-generated method stub
				 String querySql = String.format("select cpc.id as id,cpc.created as created,cpc.deleted as deleted,cpc.typeName as typeName,cpc.courseName as courseName,cpc.prepareStatus as prepareStatus,cpc.courseStatus as courseStatus," +
			                " cpc.beginTime as beginTime,cpc.endTime as endTime,cpc.courseDate as courseDate,cpc.courseType as courseType,cpc.courseId as courseId," +
			                " cpc.classesId as classesId,cpc.areaId as areaId,cpc.coursePlanTopicId as coursePlanTopicId,cpc.beginCourseTeacherId as beginCourseTeacherId," +
			                " cpc.beginCourseTime as beginCourseTime,cpc.category as category" +
			                " from course_plan_classes cpc where date(cpc.courseDate)=date('%s') and cpc.classesId='%s' order by cpc.beginTime asc", DateUtil.convertStr(lessonDate),classesId);
			        List<CoursePlanClasses> list = sqlObjectList(querySql,CoursePlanClasses.class);
			        return list;
	}
}
